﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Benefit.Service.ServerData
{
    /// <summary>
    /// 账户出入金
    /// </summary>
    public class ServiceT_CashIO_History : Benefit.Interface.ServerData.IT_CashIO_History
    {
        Benefit.DB.DBManager db = null;
        public ServiceT_CashIO_History(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 查询结算后的出入金情况
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_CashIO_History> GetInfo(string pdate)
        {
            List<Models.ServerData.T_CashIO_History> ios = new List<Models.ServerData.T_CashIO_History>();
            Interface.ServerData.IT_Settle_History itsh = new ServerData.ServiceT_Settle_History(db);
            //  T_Settle_History tsh = new T_Settle_History();
            int settlementid = itsh.GetDayMaxSettlementInfoId(pdate);
            if (settlementid != -1)
            {
                ios = GetCashIOHistoryInfo(settlementid, pdate);
            }
            return ios;
        }

        /// <summary>
        /// 查询某天最大结算编号的出入金记录
        /// </summary>
        /// <param name="settlementid">结算编号</param>
        /// <param name="pdate">日期</param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_CashIO_History> GetCashIOHistoryInfo(int settlementid, string pdate)
        {
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            System.Collections.Generic.List<Models.ServerData.T_CashIO_History> ios = new System.Collections.Generic.List<Models.ServerData.T_CashIO_History>();
            string sql = "select * from T_CashIO_History where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
            dynamic sdr = dbmanager.ExecuteReader(sql);
            while (sdr.Read())
            {
                Models.ServerData.T_CashIO_History c = new Models.ServerData.T_CashIO_History();
                c.AccountId = Convert.ToInt32(sdr["accountid"]);
                c.Amount = Convert.ToDouble(sdr["Amount"]);
                c.Credit = Convert.ToDouble(sdr["Credit"]);
                c.Force = sdr["force"].ToString();
                c.Id = Convert.ToInt32(sdr["Id"]);
                c.Mortgage = Convert.ToDouble(sdr["Mortgage"]);
                c.Pdate = Convert.ToDateTime(sdr["pdate"]);
                c.SettlementId = Convert.ToInt32(sdr["SettlementId"]);
                c.TradingDay = sdr["TradingDay"].ToString();
                ios.Add(c);
            }
            sdr.Close();
            dbmanager.Commit();
            dbmanager.Dispose();
            return ios;
        }

        /// <summary>
        /// 每天都需要执行的把Server中的数据导入到分析系统中的方法
        /// </summary>
        /// <param name="pdate">日期</param>
        public void CopyServerCashIOHistoryDataIntoBenefit(string pdate)
        {
            Interface.ServerData.IT_CashIO_History ich = new ServerData.ServiceT_CashIO_History(db);
            List<Models.ServerData.T_CashIO_History> historys = ich.GetInfo(pdate);
            foreach (Models.ServerData.T_CashIO_History ha in historys)
            {
                db.CashIOHistorys.Add(ha);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 删除某日的所有出入金记录
        /// </summary>
        /// <param name="pdate"></param>
        public void DeleteDayCashIOFromBenefit(string pdate)
        {
            var historys = from t in db.CashIOHistorys where t.TradingDay.Equals(pdate) select t;
            foreach (Models.ServerData.T_CashIO_History ha in historys)
            {
                db.CashIOHistorys.Remove(ha);
            }
            db.SaveChanges();
        }
    }
}